package com.frxuexi.park2212.payment

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.PayBeanItem
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityPayListBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * 收费项目列表页
 */
@Route(path = "/app/PayListActivity")
@AndroidEntryPoint
class PayListActivity : BaseActivity<ActivityPayListBinding,PayViewModel>() {
    val payAdapter by lazy { PayAdapter() }
    val list = mutableListOf<PayBeanItem>()
    override fun initData() {
        vm.getPays.observe(this){
            Log.i("===",it.toString())
            list.clear()
            payAdapter.data.clear()
            list.addAll(it.filter { it.paymentStatus=="未缴费" })
            payAdapter.addData(list.filter { it.effectiveDate.split("-")[1]==vm.getMonth() })
            payAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.headPaylist.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("收银台")
            headerAdd.visibility=View.GONE
        }
        binding.rvPaylist.apply {
            adapter=payAdapter
            layoutManager=LinearLayoutManager(this@PayListActivity)
        }

        payAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/PayDetailActivity").navigation()
        }

        binding.rgDate.setOnCheckedChangeListener { group, checkedId ->
            payAdapter.data.clear()
            if(checkedId==R.id.rb_month){
                payAdapter.addData(list.filter { it.effectiveDate.split("-")[1]==vm.getMonth() })
                binding.rbMonth.setTextColor(resources.getColor(R.color.white))
                binding.rbYear.setTextColor(resources.getColor(R.color.black))
            }else{
                payAdapter.addData(list.filter { it.effectiveDate.split("-")[0]==vm.getYear() })
                binding.rbMonth.setTextColor(resources.getColor(R.color.black))
                binding.rbYear.setTextColor(resources.getColor(R.color.white))
            }
            payAdapter.notifyDataSetChanged()
        }

        val types = mutableListOf<String>("全部", "物业费", "水电费","wuye")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, types)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.paylistProject.adapter=adapter
        binding.paylistProject.onItemSelectedListener=object :OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                payAdapter.data.clear()
                if(position==0){
                    payAdapter.addData(list.filter { it.effectiveDate.split("-")[1]==vm.getMonth() })
                }else{
                    payAdapter.addData(list.filter { it.effectiveDate.split("-")[1]==vm.getMonth() }.filter { it.chargeType==types[position] })
                }
                payAdapter.notifyDataSetChanged()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }
    }

    override fun onResume() {
        super.onResume()
        vm.getPays(SPUtils.getInstance().getInt(Const.UID))
    }

}